package com.yuting.client;

import com.yuting.model.*;

import java.io.IOException;
import java.util.Scanner;

/**
 * @author cyt
 * @date 2020/6/28
 */
public class ClientView {

    private ClientInitClose clientInitClose;

    public ClientView(ClientInitClose clientInitClose){
        this.clientInitClose = clientInitClose;
    }

    /**
     * 实现客户端主界面的绘制
     */
    public void clientMainPage() throws IOException, ClassNotFoundException {
        while (true){
            System.out.println("\n\n\t\t\t在线考试系统");
            System.out.println("--------------------------------------");
            System.out.print("\t[1] 学员系统");
            System.out.println("\t[2] 管理员系统");
            System.out.println("\t[0] 退出系统");
            System.out.println("--------------------------------------");
            System.out.println("请选择要进行的业务:");
            String option = ClientScanner.getScanner().next();
            switch (option){
                case "1":
                    System.out.println("进入学员系统");
                    clientStudentLogin();
                    break;
                case "2":
                    System.out.println("进入管理员系统");
                    clientManagerLogin();
                    System.out.println("发送管理员信息成功！");
                    break;
                case "0":
                    System.out.println("退出系统");
                    System.exit(1);
                    break;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }


    }


    /**
     * 学生登录之后的功能
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void clientStudentLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入学生的账户信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入学生的密码信息：");
        String password = ClientScanner.getScanner().next();

        //先验证学生账号
        UserMessage um = new UserMessage("managerCheck", new User(username,password));

        //发送userMessage给服务器
        clientInitClose.getOos().writeObject(um);

        //接收服务器的信息
        um = (UserMessage) clientInitClose.getOis().readObject();

        if("success".equals(um.getType())){
            System.out.println("亲爱的学员，登录成功");
            while (true){
                System.out.println("您可以选择考试[1]和查询成绩[2]以及返回上一级[3], 请您选择：");
                String selection = ClientScanner.getScanner().next();
                StudentFunctionMessage sfm;
                switch (selection){
                    case "1":
                        System.out.println("考试开始！请依次答题");

                        sfm = new StudentFunctionMessage("start");
                        clientInitClose.getOos().writeObject(sfm);
                        while (true){
                            sfm = (StudentFunctionMessage)clientInitClose.getOis().readObject();
                            if("test".equals(sfm.getType())){
                                System.out.println(sfm.getQuestion());
                                System.out.println("请输入您的答案：");
                                String answer = ClientScanner.getScanner().next();
                                sfm.setType("answer");
                                sfm.setAnswer(answer);
                                clientInitClose.getOos().writeObject(sfm);
                            }else if("submit?".equals(sfm.getType())){
                                System.out.println("已完成最后一题，您可以输入yes结束考试或者输入题号重新答题！");
                                String s = ClientScanner.getScanner().next();
                                if("yes".equals(s)){

                                    sfm.setType("submit");
                                    clientInitClose.getOos().writeObject(sfm);
                                    sfm = (StudentFunctionMessage)clientInitClose.getOis().readObject();
                                    if("success_submit".equals(sfm.getType())){
                                        System.out.println("考试结束！");
                                    }
                                    break;
                                }else if (s.matches("\\d+")){

                                    sfm.setType(s);
                                    clientInitClose.getOos().writeObject(sfm);
                                    sfm = (StudentFunctionMessage)clientInitClose.getOis().readObject();
                                    System.out.println("请重新作答该题：");
                                    System.out.println(sfm.getQuestion());
                                    System.out.println("请输入您的答案：");
                                    String answer = ClientScanner.getScanner().next();
                                    sfm.setType("answer");
                                    sfm.setAnswer(answer);
                                    clientInitClose.getOos().writeObject(sfm);
                                }
                            }
                        }
                        break;
                    case "2":
                        sfm = new StudentFunctionMessage("gradeCheck");
                        clientInitClose.getOos().writeObject(sfm);
                        sfm = (StudentFunctionMessage) clientInitClose.getOis().readObject();
                        System.out.println("您的学号为："+um.getUser().getUsername()+", 考试成绩为："+sfm.getScore());
                        break;
                    case "3":
                        System.out.println("再见！");
                        sfm = new StudentFunctionMessage("exit");
                        clientInitClose.getOos().writeObject(sfm);
                        return;
                    default:
                        System.out.println("该输入不符合要求，请重新输入！");

                }
            }
        }else {
            System.out.println("用户名或者密码错误，请重新尝试！");
        }


    }

    /**
     * 管理员登录之后的功能
     * @throws IOException
     * @throws ClassNotFoundException
     */
    private void clientManagerLogin() throws IOException, ClassNotFoundException {
        System.out.println("请输入管理员的账户信息：");
        String username = ClientScanner.getScanner().next();
        System.out.println("请输入管理员的密码信息：");
        String password = ClientScanner.getScanner().next();

        UserMessage um = new UserMessage("managerCheck", new User(username,password));

        //发送userMessage给服务器
        clientInitClose.getOos().writeObject(um);

        //接收服务器的信息
        um = (UserMessage) clientInitClose.getOis().readObject();

        //若成功登录，开始管理员的权限
        if("success".equals(um.getType())){
            System.out.println("登录成功，欢迎！");
            while (true){
                System.out.println("您可以管理学生信息[1]和考题信息[2]以及返回上一级[3] 请选择：");
                String option = ClientScanner.getScanner().next();
                switch (option){
                    case "1":
                        StudentMessage sm;
                        Student student;
                        int studentNum;
                        int age;
                        String name;
                        String pwd;
                        System.out.println("欢迎进入学生管理系统!");
                        System.out.println("[1] 增加一名学生 [2] 删除一名学生");
                        System.out.println("[3] 修改一名学生 [4] 查询一名学生的信息");
                        System.out.println("请做出选择：");
                        String decision = ClientScanner.getScanner().next();
                        switch (decision){
                            case "1":
                                System.out.println("请输入学生学号：");
                                studentNum = ClientScanner.getScanner().nextInt();
                                System.out.println("请输入学生密码:");
                                pwd = ClientScanner.getScanner().next();
                                System.out.println("请输入学生姓名： ");
                                name = ClientScanner.getScanner().next();
                                System.out.println("请输入学生年龄： ");
                                age = ClientScanner.getScanner().nextInt();
                                student = new Student(pwd,age,studentNum,name);
                                sm = new StudentMessage("add",student);
                                clientInitClose.getOos().writeObject(sm);
                                sm = (StudentMessage) clientInitClose.getOis().readObject();
                                if(sm.getType().equals("success")){
                                    System.out.println("加入学生成功！");
                                }else {
                                    System.out.println(sm.getType());
                                }
                                break;
                            case "2":
                                System.out.println("请输入学生学号：");
                                studentNum = ClientScanner.getScanner().nextInt();
                                student = new Student(studentNum);
                                sm = new StudentMessage("delete",student);
                                clientInitClose.getOos().writeObject(sm);
                                clientInitClose.getOos().writeObject(sm);
                                sm = (StudentMessage) clientInitClose.getOis().readObject();
                                if(sm.getType().equals("success")){
                                    System.out.println("删除学生成功！");
                                }else {
                                    System.out.println(sm.getType());
                                }
                                break;

                            case "3":
                                System.out.println("请输入学生学号：");
                                studentNum = ClientScanner.getScanner().nextInt();
                                System.out.println("请输入学生密码:");
                                pwd = ClientScanner.getScanner().next();
                                System.out.println("请输入学生姓名： ");
                                name = ClientScanner.getScanner().next();
                                System.out.println("请输入学生年龄： ");
                                age = ClientScanner.getScanner().nextInt();
                                student = new Student(pwd,age,studentNum,name);
                                sm = new StudentMessage("modify",student);
                                clientInitClose.getOos().writeObject(sm);
                                sm = (StudentMessage) clientInitClose.getOis().readObject();
                                if(sm.getType().equals("success")){
                                    System.out.println("修改学生信息成功！");
                                }else {
                                    System.out.println(sm.getType());
                                }
                                break;
                            case "4":
                                System.out.println("请输入学生学号：");
                                studentNum = ClientScanner.getScanner().nextInt();
                                student = new Student(studentNum);
                                sm = new StudentMessage("search",student);
                                clientInitClose.getOos().writeObject(sm);
                                clientInitClose.getOos().writeObject(sm);
                                sm = (StudentMessage) clientInitClose.getOis().readObject();
                                if(sm.getType().equals("success")){
                                    System.out.println("查询学生成功！");
                                    System.out.println(sm.getStudent());
                                }else {
                                    System.out.println(sm.getType());
                                }
                                break;
                        }
                        break;
                    case "2":
                        int questionNum;
                        String questionContent;
                        String correctAnswer;
                        Question q;
                        QuestionMessage qm;
                        System.out.println("欢迎来到考题管理系统");
                        System.out.println();
                        System.out.println("[1] 增加一道考题 [2] 删除一道考题");
                        System.out.println("[3] 修改一道考题 [4] 查询一道考题的信息");
                        System.out.println("请做出选择：");
                        String decision1 = ClientScanner.getScanner().next();
                        switch (decision1){
                            case "1":
                                System.out.println("请输入考题编号：");
                                questionNum = ClientScanner.getScanner().nextInt();
                                System.out.println("请输入考题题目内容： ");
                                questionContent = ClientScanner.getScanner().next();
                                System.out.println("请输入考题答案： ");
                                correctAnswer = ClientScanner.getScanner().next();
                                q = new Question(questionNum,questionContent,correctAnswer);
                                qm = new QuestionMessage("add", q);
                                clientInitClose.getOos().writeObject(qm);
                                Object temp;
                                if((temp = clientInitClose.getOis().readObject()) instanceof QuestionMessage ){
                                    System.out.println("转化成功!");
                                    qm = (QuestionMessage) temp;
                                }

                                if(qm.getType().equals("success")){
                                    System.out.println("添加考题成功！");
                                }else {
                                    System.out.println(qm.getType());
                                }
                                break;

                            case "2":
                                System.out.println("请输入考题编号：");
                                questionNum = ClientScanner.getScanner().nextInt();
                                q = new Question(questionNum);
                                qm = new QuestionMessage("delete", q);
                                clientInitClose.getOos().writeObject(qm);
                                qm = (QuestionMessage) clientInitClose.getOis().readObject();
                                if(qm.getType().equals("success")){
                                    System.out.println("删除考题成功！");
                                }else {
                                    System.out.println(qm.getType());
                                }
                                break;

                            case "3":
                                System.out.println("请输入考题编号：");
                                questionNum = ClientScanner.getScanner().nextInt();
                                System.out.println("请输入考题题目内容： ");
                                questionContent = ClientScanner.getScanner().next();
                                System.out.println("请输入考题答案： ");
                                correctAnswer = ClientScanner.getScanner().next();
                                q = new Question(questionNum,questionContent,correctAnswer);
                                qm = new QuestionMessage("modify", q);
                                clientInitClose.getOos().writeObject(qm);
                                qm = (QuestionMessage) clientInitClose.getOis().readObject();
                                if(qm.getType().equals("success")){
                                    System.out.println("修改考题成功！");
                                }else {
                                    System.out.println(qm.getType());
                                }
                                break;

                            case "4":
                                System.out.println("请输入考题编号：");
                                questionNum = ClientScanner.getScanner().nextInt();
                                q = new Question(questionNum);
                                qm = new QuestionMessage("search", q);
                                clientInitClose.getOos().writeObject(qm);
                                qm = (QuestionMessage) clientInitClose.getOis().readObject();
                                if(qm.getType().equals("success")){
                                    System.out.println("查找考题成功！");
                                    System.out.println(qm.getQuestion());
                                }else {
                                    System.out.println(qm.getType());
                                }
                                break;
                            default:
                        }
                        break;
                    case "3":
                        System.out.println("再见！");
                        um.setType("exit");
                        clientInitClose.getOos().writeObject(um);
                        return;
                    default:
                        System.out.println("没有这个选项，请重新选择!");
                }
            }

        }else {
            System.out.println("用户名或者密码错误，请重新尝试！");
        }
    }
}
